package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.pojos.ApUserSearch;
import com.heima.search.service.ApUserSearchService;
import com.heima.search.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lzy
 * @date 2022/7/26 1:22
 */
@Service
@Slf4j
public class ApUserSearchServiceImpl implements ApUserSearchService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存搜索记录

    @Override
    public void insert(String keyword, Integer userId) {
        //用户id判空
        if (userId == null){
            log.warn("用户id不能为空");
            return;
        }
        //创建查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("keyword").is(keyword));
        //查询结果
        ApUserSearch apUserSearch = mongoTemplate.findOne(query, ApUserSearch.class);
        //对查询结果进行判空
        if (apUserSearch == null){
            //声明apusersearch类
            ApUserSearch search = new ApUserSearch();
            //向search内赋值
            search.setUserId(userId);
            search.setKeyword(keyword);
            search.setCreatedTime(new Date());
            //保存
            mongoTemplate.save(search);
        }
    }

    //查询搜索记录

    @Override
    public ResponseResult findUserSearch() {
        //先获取用户信息
        ApUser user = AppThreadLocalUtil.getUser();
        //判断用户是否登录
        if (user == null){
            log.warn("用户为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取用户id
        Integer id = user.getId();
        //构造查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        //查询按照创建时间倒序查询
        query.with(Sort.by("createTime").descending());
        query.limit(10);
        //查询ApUserSearch这个表的条件
        List<ApUserSearch> records = mongoTemplate.find(query, ApUserSearch.class);

        return ResponseResult.okResult(records);
    }

    //删除搜索记录

    @Override
    public ResponseResult delUserSearch(HistorySearchDto historySearchDto) {
        if (historySearchDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String id = historySearchDto.getId();
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        mongoTemplate.remove(query,ApUserSearch.class);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
